<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>购买原材料</title>

    <script src="util.js"></script>
    <script src="./req.js"></script>
    <style>
        html {
            background: #EEE;
            padding: 20px;
        }
        
        .container {
            margin-top: 15px;
            background: #FFF;
            height: 500px;
            flex-direction: column;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        
        .item {
            display: flex;
            justify-content: space-between;
            font-size: 30px;
        }
        
        .item>input {
            width: 300px;
            font-size: 28px;
        }
    </style>

</head>

<body>
    <div style="font-size: 28px; font-weight: 600;">工厂资金：<span id="item">0</span></div>
    <div class="container">
        <div class="item">
            <span>位置</span>
            <input type="number" id="g1">
        </div>
        <div class="item" style="margin-top: 10px;">
            <span>供应原材料ID</span>
            <input type="number" id="g2">
        </div>
        <div class="item" style="margin-top: 10px; justify-content: center;">

            <button style="width: 200px; height: 50px;" onclick="buy()">提交</button>
        </div>
    </div>

</body>
<script type="text/javascript">
    initPrice()

    function initPrice() {
        JQ("http://192.168.1.101:8085/dataInterface/UserWorkInfo/getAll")
            .then(e => {
                $("#item").html(e.data[0].price)
            })
    }

    function buy() {
        console.log($("#g1").val(), $("#g2").val());
        if (!$("#g1").val() || !$("#g2").val()) {
            alert("请输入完整")
            return
        }
        if ($("#g1").val() > 3 || $("#g1").val() < 0) {
            alert("请输入0~3")
            return
        }

        JQ("http://192.168.1.101:8085/dataInterface/UserProductionLine/search", {
                position: $("#g1").val()
            })
            .then(e => {
                JQ("http://192.168.1.101:8085/Interface/index/getMaterial")
                    .then(e2 => {
                        var len = 0;
                        e2.data.forEach((item, index) => {
                            len++;
                            if (item.id == $("#g2").val()) {
                                toBuy(e.data[0].id, 1, item.supplyListId);
                                throw Error("跳出循环")
                            }
                            if (len == e2.data.length) {
                                alert("没有此商品")
                            }
                        });
                    })
            })
    }

    function toBuy(userLineId, num, supplyListId) {

        JQ("http://192.168.1.101:8085/Interface/index/addUserMaterialStore", {
                userLineId,
                num,
                supplyListId
            })
            .then(e => {

                alert(e.message)

                if (e.status == 200) {
                    initPrice();
                    $("#g1").val("")
                    $("#g2").val("")
                }
            })
    }
</script>

</html>